import 'package:english_words/english_words.dart';
import 'package:flu_framework/common/base/BaseWidget.dart';
import 'package:flu_framework/ui/home/model/list_data.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Refresh2Page extends BaseWidget {
  const Refresh2Page({super.key});

  @override
  BaseWidgetState<BaseWidget> getState() => _Refresh2PageState();
}

class _Refresh2PageState extends BaseWidgetState<Refresh2Page> {

  ListData _listData = ListData();
  int currentPage = 1;

  @override
  String setPageTitle() => "上下拉加载2";

  @override
  void initState() {
    super.initState();
    _listData.totalPage = 5;
    _refreshData();
  }

  @override
  Widget buildContent() {
    return _createCupertinoSliverRefreshControl();
  }

  Widget _createCupertinoSliverRefreshControl(){
    return CustomScrollView(
      physics: BouncingScrollPhysics(),
      slivers: [
        CupertinoSliverRefreshControl(
          onRefresh: () async {
            // await Future.delayed(Duration(seconds: 3));
            await _refreshData();
          },
          builder: (BuildContext context,
              RefreshIndicatorMode refreshState,
              double pulledExtent,
              double refreshTriggerPullDistance,
              double refreshIndicatorExtent,){
            return Container(
              color: Colors.cyan,
              height: 70,
              alignment: Alignment.center,
              child: AnimatedOpacity(
                duration: Duration(seconds: 2),
                opacity: 0.8,
                child: Text(
                    _getRefreshState(refreshState,pulledExtent),
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20
                  ),
                ),
              ),
            );
          },
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (context,index){
              if(_listData.data.length-1>index) {
                return ListTile(
                  title: Text(_listData.data[index]),
                );
              }else{
                if(currentPage < _listData.totalPage) {
                  _loadData();
                  return  Container(
                    color: Colors.orange,
                    height: 70,
                    alignment: Alignment.center,
                    child: Text(
                      "正在加载...",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 20
                      ),
                    ),
                  );
                }else{
                  return  Container(
                    color: Colors.orange,
                    height: 70,
                    alignment: Alignment.center,
                    child: Text(
                      "上拉加载完毕",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 20
                      ),
                    ),
                  );
                }
              }

            },
            childCount: _listData.data.length,
          ),
        ),
      ],
    );
  }

  _refreshData() async {
    _listData.data.clear();
    currentPage = 1;
    await Future.delayed(Duration(seconds: 2)).then((value){
      setState(() {
        _listData.data.insertAll(0,
            nouns.take(20).map((e) => e.toString()).toList());
      });
    });
  }

  _loadData() async {
    currentPage++;
    await Future.delayed(Duration(seconds: 2)).then((value){
      setState(() {
        _listData.data.insertAll(_listData.data.length-1,
            nouns.take(20).map((e) => e.toString()).toList());
      });
    });
  }

  String _getRefreshState(RefreshIndicatorMode refreshState,double pulledExtent,) {
    String state = "";
    switch(refreshState) {
      case RefreshIndicatorMode.drag:
        state = "正在拉";
        break;
      case RefreshIndicatorMode.refresh:
        state = "正在刷新...";
        break;
      case RefreshIndicatorMode.armed:
        state = "准备刷";
        break;
      case RefreshIndicatorMode.done:
        state = "刷新完成";
        break;
    }
    return state;
  }

  /// material风格下载加载
  Widget _createRefreshIndicator(){
    return RefreshIndicator(
      backgroundColor: Colors.green,
      color: Colors.redAccent,
      onRefresh: () async {
        _refreshData();
      },
      child: ListView.separated(
          itemBuilder: (context,index){
            return ListTile(
              title: Text(_listData.data[index]),
            );
          },
          separatorBuilder: (context,index){
            return Divider(height: 1,);
          },
          itemCount: _listData.data.length),
    );
  }

}
